import csv
import re

import requests

url = "https://movie.douban.com/top250"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}

res = requests.get(url, headers=headers)
page_content = res.text

obj = re.compile(r'<li>.*?<span class="title">(?P<name>.*?)</span>'
                 r'.*?<p class="">.*?<br>(?P<year>.*?)&nbsp;'
                 r'.*?<div class="star">.*?<span>(?P<pj>.*?)人评价</span>'
                 r'.*?<p class="quote">.*?<span class="inq">(?P<ms>.*?)</span>', re.S)

result = obj.finditer(page_content)
f = open('./data.csv', 'w', encoding='utf-8')
csvwrite = csv.writer(f)
for i in result:
    # print(i.group('name'))
    # print(i.group('pj'))
    # print(i.group('ms'))
    # print(i.group('year').strip()) # strip 去除空白字符
    dict = i.groupdict()
    dict['year'] = i.group('year').strip()
    csvwrite.writerow(dict.values())


f.close()
res.close()
print('over')
